Spring Boot এ gRPC Client তৈরি করা

Java Technologies - স্প্রিং বুট ক্লায়েন্ট (Spring Boot Client) Spring Boot এবং gRPC Client Integration |
114
114

gRPC একটি উচ্চ-প্রদর্শন মাইক্রোসার্ভিস RPC (Remote Procedure Call) ফ্রেমওয়ার্ক, যা Protobuf ব্যবহার করে ডেটা সিরিয়ালাইজেশন করে। Spring Boot-এ gRPC Client তৈরি করার ধাপ নিচে দেয়া হলো।


1. প্রয়োজনীয় ডিপেন্ডেন্সি যোগ করা

Spring Boot প্রজেক্টে gRPC ব্যবহার করতে grpc-spring-boot-starter ডিপেন্ডেন্সি যোগ করতে হবে।

Maven:

<dependency>
    <groupId>net.devh</groupId>
    <artifactId>grpc-spring-boot-starter</artifactId>
    <version>2.14.0</version>
</dependency>

Gradle:

implementation 'net.devh:grpc-spring-boot-starter:2.14.0'

2. Proto ফাইল তৈরি করা

gRPC সেবা ব্যবহারের জন্য .proto ফাইল তৈরি করুন। নিচে একটি উদাহরণ দেয়া হলো:

syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.example.grpc";
option java_outer_classname = "GreetingProto";

service GreetingService {
    rpc Greet (GreetingRequest) returns (GreetingResponse);
}

message GreetingRequest {
    string name = 1;
}

message GreetingResponse {
    string message = 1;
}

3. Proto ফাইল কম্পাইল করা

Proto ফাইল থেকে Java ক্লাস জেনারেট করতে protobuf-maven-plugin ব্যবহার করুন।

Maven Plugin Configuration:

<plugin>
    <groupId>org.xolstice.maven.plugins</groupId>
    <artifactId>protobuf-maven-plugin</artifactId>
    <version>0.6.1</version>
    <configuration>
        <protocArtifact>com.google.protobuf:protoc:3.21.7:exe:${os.detected.classifier}</protocArtifact>
        <pluginId>grpc-java</pluginId>
        <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.54.0:exe:${os.detected.classifier}</pluginArtifact>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>compile-custom</goal>
            </goals>
        </execution>
    </executions>
</plugin>

mvn clean install রান করলে Java ফাইলগুলো জেনারেট হবে।


4. gRPC Client তৈরি করা

Spring Boot-এ gRPC ক্লায়েন্ট তৈরি করতে আমরা @GrpcClient এনোটেশন ব্যবহার করব।

import com.example.grpc.GreetingServiceGrpc;
import com.example.grpc.GreetingProto.GreetingRequest;
import com.example.grpc.GreetingProto.GreetingResponse;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

@Service
public class GreetingClient {

    @GrpcClient("greeting-service")
    private GreetingServiceGrpc.GreetingServiceBlockingStub greetingServiceStub;

    public String getGreeting(String name) {
        GreetingRequest request = GreetingRequest.newBuilder()
                .setName(name)
                .build();
        GreetingResponse response = greetingServiceStub.greet(request);
        return response.getMessage();
    }
}

5. gRPC সার্ভারের সাথে যোগাযোগ

@RestController বা অন্য কোনও কম্পোনেন্ট থেকে gRPC ক্লায়েন্ট ব্যবহার করতে পারেন।

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private final GreetingClient greetingClient;

    public GreetingController(GreetingClient greetingClient) {
        this.greetingClient = greetingClient;
    }

    @GetMapping("/greet")
    public String greet(@RequestParam String name) {
        return greetingClient.getGreeting(name);
    }
}

6. gRPC সার্ভার ঠিকানা কনফিগার করা

application.yml বা application.properties ফাইলে gRPC সার্ভারের ঠিকানা যোগ করুন:

grpc:
  client:
    greeting-service:
      address: "localhost:6565"

7. Spring Boot অ্যাপ্লিকেশন চালানো

Spring Boot অ্যাপ্লিকেশন চালান এবং /greet?name=John URL-এ অ্যাক্সেস করুন। এটি gRPC সার্ভারে একটি রিকুয়েস্ট পাঠাবে এবং সার্ভারের রেসপন্স রিটার্ন করবে।


সংক্ষেপে:

  1. gRPC প্রটোকল ব্যবহার করে Spring Boot-এ উচ্চ-দক্ষতাসম্পন্ন ক্লায়েন্ট তৈরি করা সম্ভব।
  2. Protobuf ব্যবহার করে ডেটা ডিফাইন ও সিরিয়ালাইজ করা হয়।
  3. gRPC ক্লায়েন্ট ইমপ্লিমেন্টেশনে Spring Boot এবং grpc-spring-boot-starter সহজ সমাধান দেয়।

এটি ব্যবহার করে আপনার মাইক্রোসার্ভিস স্থাপনার কার্যকারিতা বাড়ানো সম্ভব।

Content added By
টপ রেটেড অ্যাপ

স্যাট অ্যাকাডেমী অ্যাপ

আমাদের অল-ইন-ওয়ান মোবাইল অ্যাপের মাধ্যমে সীমাহীন শেখার সুযোগ উপভোগ করুন।

ভিডিও
লাইভ ক্লাস
এক্সাম
ডাউনলোড করুন
Promotion